<?php

class model_user extends Model
{
	function __construct()
	{
		parent::__construct();
		
	}
	
	function update_last_login($userId)
	{
		$this->db->where("id", $userId)
				 ->update("user", array("lastlogin"=> date( 'Y-m-d H:i:s') ))	;
	
	}
	
	function isValidUser($data)
	{
		$this->db	->select("*")
					->from("user")
					->where($data);
		$r = $this->db->get();
		
		if(count($r) == 1)
		{
			return $r->result_array();
		}
		else{
			return 0;	
		}
	}
	
	function getUsers($limit=99999,$order = array("user.created", "desc"), $where = array())
	{
		$where['company_id'] = $_SESSION['user_company'];
		
		$this->db	->select("user.firstname
							  ,user.lastname
							  ,user.username
							  ,user.lastlogin
							  ,user.id
							  ,user.user_type
							  ,lookup.lu_lable as user_type_lbl
							  ,c.company_name")
					->from("user")
					->join("lookup", "lookup.lu_value = user.user_type and lookup.lu_type='user_type'", "left")
					->join("company c", "c.id = user.company_id and c.active=1", "left")
					->group_by("user.id")
					->order_by($order[0], $order[1]);
		if(!empty($where))
		{
			$this->db->where($where);
		}			
		
		$r = $this->db->get();
		//echo $this->db->last_query();
		return $r->result_array();	
	}
	
	function newUser($data)
	{
		$this->db->insert("user", $data);
		return $this->db->insert_id();	
	}
	
	
	function removeUser($id)
	{
		$this->updateUser(array("active" => 0), $id);
	}
	
	
	function updateUser($data, $id)
	{
		$this->db->update("user", $data, array("id" => $id));		
	}
	
	function getUserData($id)
	{
		$this->db->select("id,firstname,lastname,user_type,lastlogin,username,company_id")
				->from("user")
				->where(array("id" => $id) );
		$r = $this->db->get();
		
		return $r->result_array();	
	}
}

